<template>
  <div>
    <el-dialog title="提示" :visible.sync="isShow" width="60%" :before-close="handleClose">
      <el-form :model="datas" :rules="rules" ref="chapter" label-width="120px">
        <el-form-item label="编号" prop="name">
          <el-input v-model="datas.name" placeholder="请输入编号"></el-input>
        </el-form-item>
        <el-form-item label="租金" prop="money">
          <el-input v-model="datas.money" placeholder="请输入租金"></el-input>
        </el-form-item>
        <el-form-item label="车辆类型" prop="type_id">
          <el-select v-model="datas.type_id" placeholder="请选择">
            <el-option v-for="item in typeList" :key="item._id" :label="item.name" :value="item._id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆状态" prop="state">
          <el-select v-model="datas.state" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="createChapter('chapter')">创建</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { updateBikeApi } from '../../../api/bike'
import { getType } from "../../../api/type";
export default {
  props: ['name', 'money', 'type_id', 'state', 'bike_id'],
  components: {},
  data () {
    return {
      typeList: [],
      book_id: this.$route.query.book_id,
      isShow: true,
      options: [
        {
          value: true,
          label: "正常",
        },
        {
          value: false,
          label: "报废",
        },
      ],
      datas: {},
      rules: {
        name: [
          {
            required: true,
            message: "编号不能为空",
            trigger: "blur",
          },
        ],
        money: [
          {
            required: true,
            message: "租金不能为空",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "车辆状态不能为空",
            trigger: "blur",
          },
        ],
        type_id: [
          {
            required: true,
            message: "车辆状态不能为空",
            trigger: "blur",
          },
        ]
      },
    }
  },
  methods: {
    // 关闭dialog
    handleClose () {
      this.$emit('closeDialog')
    },
    createChapter (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateBikeApi({
            _id: this.$props.bike_id,
            ...this.datas,
          }).then((res) => {
            this.typeDate = {};
            this.$message({
              message: "修改成功",
              type: "success",
            });
            this.$emit('updateSuccess')
          });
          // 发送请求
        } else {
          return false;
        }
      })

    },
  },
  mounted () {
    this.datas = {
      name: this.$props.name,
      money: this.$props.money,
      type_id: this.$props.type_id,
      state: this.$props.state
    }

    getType().then(res => {
      if (res.success) {
        this.typeList = res.result
      }
    })
  }
}
</script>
<style scoped></style>
